Imports System.ComponentModel
Imports System.Web.UI
Imports System.Reflection
Imports System.Web.UI.HtmlControls
Imports System.Collections.Specialized
Imports System.Configuration
Imports System.Threading
Imports System.Globalization
Imports System
Imports Objects.EmployeeInfo
Imports Objects.Security
Imports System.Web.UI.WebControls


<DefaultProperty("Text"), ToolboxData("<{0}:Banner runat=server></{0}:Banner>"), PersistChildren(True)> Public Class Banner
    Inherits System.Web.UI.WebControls.CompositeControl

    Private WithEvents mnuMain As Menu

    Private msAppName As String = String.Empty
    Private mbAutoLoad As Boolean = True
    Protected Overrides Sub CreateChildControls()
       
        Menu.EnableViewState = False
        Menu.ForeColor = Drawing.Color.White
        Menu.StaticHoverStyle.BackColor = Drawing.Color.LightSteelBlue
        Menu.StaticHoverStyle.BorderColor = Drawing.Color.RoyalBlue
        Menu.StaticHoverStyle.BorderStyle = WebControls.BorderStyle.Solid
        Menu.StaticHoverStyle.BorderWidth = New WebControls.Unit(1)
        Menu.StaticEnableDefaultPopOutImage = False
        Menu.DynamicMenuStyle.BackColor = Drawing.Color.FromName("#999999")

        Menu.DynamicMenuStyle.HorizontalPadding = New WebControls.Unit(5)
        Menu.DynamicMenuStyle.VerticalPadding = New WebControls.Unit(2)

        Menu.DynamicHoverStyle.BackColor = Drawing.Color.LightSteelBlue
        Menu.DynamicHoverStyle.BorderColor = Drawing.Color.RoyalBlue
        Menu.DynamicHoverStyle.BorderStyle = WebControls.BorderStyle.Solid
        Menu.DynamicHoverStyle.BorderWidth = New WebControls.Unit(1)
        Controls.Add(Menu)
    End Sub
    Public Sub CreateMenu()

        Menu.Items.Clear()
        Menu.Orientation = Orientation.Horizontal
        Dim loDS As DataSet = CType(Page, Objects.cPage).CurrentUser.GetUserPermissions(Page.Request.Url.LocalPath)
        Dim loRow As DataRow
        Dim loChildRow As DataRow
        Dim loMenuItem As MenuItem

        If loDS.Tables.Count > 0 Then
            Dim ldtStart As DataTable = New DataTable
            Dim loTable As DataTable = loDS.Tables(0)
            Dim loChildRows() As DataRow

            For Each loRow In loTable.Rows
                loMenuItem = New MenuItem(loRow("Description").ToString)
                loMenuItem.ImageUrl = "~/_includes/images/RootLeafImage.gif"
                Menu.Items.Add(loMenuItem)
                If Not IsDBNull(loRow("Href")) Then
                    If loRow("Href").ToString.ToLower.Length > 0 Then
                        SetMenuItemHref(loMenuItem, loRow("Href").ToString)

                    End If
                End If
                If Not IsDBNull(loRow("Target")) Then
                    loMenuItem.Target = loRow("Target").ToString
                End If
                loChildRows = loRow.GetChildRows(loRow("Name").ToString)
                For Each loChildRow In loChildRows
                    CreateSubMenu(loChildRow, loMenuItem)
                Next
            Next
        End If
    End Sub
    Private Sub SetMenuItemHref(ByVal poMenuItem As MenuItem, ByVal psHref As String)
        If psHref.Length > 0 Then
            Dim lbEncrypt As Boolean
            If psHref.ToString.ToLower.IndexOf("reportserver") <> -1 Then
                lbEncrypt = False
            ElseIf psHref.ToString.ToLower.IndexOf("http") <> -1 Then
                If psHref.ToString.ToLower = Page.Request.Url.Host.ToLower Then
                    lbEncrypt = True
                Else
                    lbEncrypt = False
                End If
            Else
                lbEncrypt = True
            End If

            If lbEncrypt Then
                poMenuItem.NavigateUrl = CType(Page, Objects.cPage).EncryptURL(psHref.ToString)
            Else
                poMenuItem.NavigateUrl = (psHref.ToString)
            End If
        End If
    End Sub


    Private Sub CreateSubMenu(ByVal poRow As DataRow, ByVal loParentMenu As MenuItem)

        Dim loRow As DataRow
        Dim loChildRows() As DataRow
        Dim loMenuItem As MenuItem

        loMenuItem = New MenuItem(poRow("Description").ToString)
        loMenuItem.ImageUrl = "~/_includes/images/LeafImageSep.gif"
        loParentMenu.ChildItems.Add(loMenuItem)

        If Not IsDBNull(poRow("Href")) Then
            SetMenuItemHref(loMenuItem, poRow("Href").ToString)
        End If
        If Not IsDBNull(poRow("Target")) Then
            loMenuItem.Target = poRow("Target").ToString
        End If
        loChildRows = poRow.GetChildRows(poRow("Name").ToString)
        For Each loRow In loChildRows
            CreateSubMenu(loRow, loParentMenu)
        Next
    End Sub
    Public ReadOnly Property Menu() As Menu
        Get
            If mnuMain Is Nothing Then
                mnuMain = New Menu
            End If
            Return mnuMain
        End Get
    End Property

    Protected Overrides Sub RenderChildren(ByVal writer As System.Web.UI.HtmlTextWriter)
        Dim loAss As Assembly = Assembly.GetExecutingAssembly
        Dim loAssembly As [Assembly] = System.Reflection.Assembly.GetExecutingAssembly
        Dim lsStream1 As New IO.StreamReader(loAssembly.GetManifestResourceStream("AjaxControlLib.BannerHTML.txt"))
        Dim lsHTML As String = lsStream1.ReadToEnd()
        Dim loSettings As Objects.Utility.Settings = New Objects.Utility.Settings()
        lsHTML = lsHTML.Replace("~AppName", ApplicationName)
        Dim employeeObject As New Employee()
        employeeObject = CType(Page, Objects.cPage).CurrentUser
        If employeeObject.IsFieldAdministrator(employeeObject.GUI) Then
            lsHTML = lsHTML.Replace("~homeurl", "/CMT/FieldAdminHome.aspx")
        Else
            lsHTML = lsHTML.Replace("~homeurl", "/CMT/CreateAnEvent.aspx")
        End If
        If Not String.IsNullOrEmpty(loSettings.GetKeyValue("Logo")) Then
            lsHTML = lsHTML.Replace("~logo", loSettings.GetKeyValue("Logo"))
        End If

        lsHTML = lsHTML.Replace("~externalapplicationname", loSettings.GetKeyValue("externalapplicationname"))
        Dim lsStream2 As New IO.StreamReader(loAssembly.GetManifestResourceStream("AjaxControlLib.BannerHTML2.txt"))
        Dim lsHTML2 As String = lsStream2.ReadToEnd()
        writer.Write(lsHTML)
        mnuMain.RenderControl(writer)
        Try
            Dim loAppSettings As AppSettingsReader = New AppSettingsReader
            Dim lsMessage As String = String.Empty
            lsMessage = loAppSettings.GetValue("Message", GetType(System.String)).ToString
            If lsMessage.Length > 0 Then
                lsHTML2 += "<tr><td colspan=3><marquee>" & lsMessage & "</marquee></td></tr>"
            End If
        Catch ex As Exception
        End Try
        writer.Write(lsHTML2)
    End Sub


    Protected Overrides Sub Render(ByVal writer As System.Web.UI.HtmlTextWriter)
        EnsureChildControls()
        Height = New WebControls.Unit(70)
        MyBase.Render(writer)
        Width = New WebControls.Unit("100%")
    End Sub

    Private Sub Banner_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
        If AutoLoad Then CreateMenu()
    End Sub
    Public Property ApplicationName() As String
        Get
            Return msAppName
        End Get
        Set(ByVal value As String)
            msAppName = value
        End Set
    End Property
    Public Property AutoLoad() As Boolean
        Get
            Return mbAutoLoad
        End Get
        Set(ByVal value As Boolean)
            mbAutoLoad = value
        End Set
    End Property
End Class
